//
//  JBWUserAccountModel.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/1.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWUserAccountModel: NSObject ,NSCoding{

    ///用户授权的唯一票据
    var access_token: String?
    ///授权用户的UID
    var uid: String?
    ///用户昵称
    var screen_name: String?
    ///用户头像
    var avatar_large:String?
    ///token过期时间
    var expires_Date: Date?
    
    ///access_token的声明周期 单位:秒
    var expires_in: Double = 0 {
        //监听外界赋值
        didSet{
            //过期日期 = 当前日期+规定时间
            expires_Date = Date(timeIntervalSinceNow: expires_in)
        }
    }
    
    //KVC构造函数
    init(dict:[String: Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    override var description: String {
        
        let keys = ["access_token","uid","screen_name","avatar_large"]
        return dictionaryWithValues(forKeys: keys).description
        
    }

    
    //归档  以二进制形式保存到磁盘
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_Date, forKey: "expires_Date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(screen_name, forKey: "screen_name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject (forKey: "access_token") as? String
        expires_Date = aDecoder.decodeObject (forKey: "expires_Date") as? Date
        uid = aDecoder.decodeObject (forKey: "uid") as? String
        screen_name = aDecoder.decodeObject (forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject (forKey: "avatar_large") as? String
        
    }
}
